package org.example.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.config.R;
import org.example.domain.Param;
import org.example.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("param")
public class ParamController {
    @Autowired
    ParamService paramService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("list")
    public R list(@RequestBody Param param, Page page){
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        List<Param> list= paramService.list(param);
        PageInfo<Param> info = new PageInfo(list);
        return R.successed(info);
    }

//    @RequestMapping("clearCach")
//    public R clearCach(){
//        redisTemplate.delete("paramList:");
//        return R.successed(null);
//    }

    @RequestMapping("add")
    public R add(@RequestBody Param param){
        paramService.add(param);
        return R.successed(null);
    }

    @RequestMapping("delByUpdate")
    public R delByUpdate(Integer id){
        paramService.delByUpdate(id);
        return R.successed(null);
    }
    @RequestMapping("updates")
    public R updates(@RequestBody Param param){
        paramService.updates(param);
        return R.successed(null);
    }
    @Autowired
    HttpServletResponse response;

    @RequestMapping("daochu")
    public void daochu() throws IOException {
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);

//自定义标题别名
        writer.addHeaderAlias("name", "参数名称");
        writer.addHeaderAlias("keyName", "参数键名");
        writer.addHeaderAlias("keyValue", "参数键值");
        writer.addHeaderAlias("systems", "系统内置");
        writer.addHeaderAlias("remark", "备注");
        writer.addHeaderAlias("times","创建时间");

       List<Param> list= paramService.lists();

        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    @RequestMapping("dellAll")
    public R delAll(Integer[] ids){
        paramService.delAll(ids);
        return R.successed(null);
    }

    @RequestMapping("getById")
    public R getById(Integer id){
       Param param= paramService.getById(id);
       return R.successed(param);
    }
}
